import { lazy } from "react";
import { createBrowserRouter, Navigate } from "react-router-dom";

// 自动收集 pages 下的页面，要求每个页面默认导出 React 组件
const modules = import.meta.glob("../pages/**/index.jsx");

const pageRoutes = Object.keys(modules).map((path) => {
  // 生成路由路径：如 ../pages/app/index.jsx -> /app
  const match = path.match(/\.\.\/pages\/(.*)\/index\.jsx$/);
  const routePath = match?.[1] ? `/${match[1]}` : "/";

  console.log(routePath, "routePath");
  
  const Component = lazy(modules[path]);
  return {
    path: routePath,
    element: <Component />,
  };
});

const routes = [
  { index: true, element: <Navigate to="/jump" replace /> },
  ...pageRoutes,
  { path: "*", element: <Navigate to="/404" replace /> },
];

export const router = createBrowserRouter(routes, {
  basename: "/bykj",
});


console.log(routes,"routes");


export default router;
